package com.zhitian.fortest;



import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.csvreader.CsvWriter;




public class GetDataUtils2 {
	//获取当前文件夹下所有 含有表内容的html文件  
	//test:"D:\\AHTMLDATA\\黄小雨862373030759292_20180207213918_报告\\Content"
	public static List<String> getAllHtml(String folderName){
		List<String> htmlFileList = new ArrayList<String>();
		boolean isMatch;
		String pattern = "PingHangReportPage.*.html";
		File input = new File(folderName);
		for (String s : input.list()) {
			isMatch = Pattern.matches(pattern, s);
			if(isMatch) {
				htmlFileList.add(s);
			}
		}
		//确保list里的html文件是 按顺序排的
		Collections.sort(htmlFileList, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return getNumber(o1) -getNumber(o2);
			}
		});
		/*for(String s : htmlFileList) {
			System.out.println(s);
			System.out.println(getNumber(s));
		}*/
		
		return htmlFileList;
	}
	//获取最后一个文件的后缀数字 例如PingHangReportPage_黄小雨862373030759292_246.html 中的246 方便后面按顺序爬取文件
	public static int getNumber(String file) {
		String regex = "(\\D*)(\\d+)(.*)";
		Pattern pattern = Pattern.compile(regex);
		Matcher matchers = pattern.matcher(file);
		if (matchers.find( ) && !matchers.group(3).equals(".html") ) {
	         String numReg="[^0-9]";
	         Pattern p = Pattern.compile(numReg);
	         Matcher matcher2 = p.matcher(matchers.group(3));
	         return Integer.parseInt(matcher2.replaceAll(""));
	        
	     } 
		return 0;
		
	}
	
	
	//判断字符串是否全为数字
	public static boolean isInteger(String str) {  
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
        return pattern.matcher(str).matches();  
    }
	//正则表达式 是否匹配  数字-数字  为QQ群聊中的对方
	public static boolean isQQGroup(String str) {
		
		Pattern pattern = Pattern.compile("^(\\d+)(-)(\\d+).*");
		return pattern.matcher(str).matches();
	}
	//匹配任何以字母开头的字符串
	public static boolean  isWxId(String str) {
		Pattern pattern = Pattern.compile("^[A-Za-z].*");
		return pattern.matcher(str).matches();
	}
	
	//创建CsvWriter
	public static CsvWriter createCsvWriter(String path,String[] header) throws IOException {
		
		FileOutputStream fos = new FileOutputStream(path);
		//在文件中增加BOM，该处的byte[] 可以针对不同编码进行修改
		fos.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
		CsvWriter writer = new CsvWriter(fos,',',Charset.forName("UTF-8"));
		//String[] header = {"序号","时间","动作","对方","内容"};
		writer.writeRecord(header);
		
		return writer;
	}
	
	//关闭CsvWrite
	public static void  closeCsvWriter(List<CsvWriter> list) {
		for(CsvWriter csvWriter : list) {
			csvWriter.flush();
			csvWriter.close();
		}
		
	}
	/**
	 * 获取应用列表
	 * @param folder  需要爬取的 html集合的文件夹
	 * @param csvPath  csv文件输出地址
	 * @throws IOException
	 */
	public static void getAppList(String folder, String csvPath) throws IOException {
		List<String> htmls = getAllHtml(folder);
		
		System.out.println("-------获取应用列表---------");
		System.out.println("被处理的文件夹："+folder);
		System.out.println("csv文件输出路径："+csvPath);
		
		//准备存储应用列表的csv
		String applist = csvPath+"应用列表.csv";
		CsvWriter writer = createCsvWriter(applist, 
				new String[] {"序号","应用","事件数量","联系人数量","文件数量"
						,"删除数量","总量"});
		
		for(String html : htmls) {
			File input = new File(folder+html);
			
			Document document = Jsoup.parse(input,"UTF-8");
			Elements datatable = document.getElementsByClass("datatable");
			
			for(Element e : datatable) {
				//每个表的第一行都是字段，根据字段的内容判断表的内容
				Element firstTr = e.getElementsByTag("tr").first();
				Elements th = firstTr.getElementsByTag("th");
				
				//应用列表  判断标准：7个字段 且第二个字段为"应用"
				if(th.size()==7 && th.get(1).ownText().equals("应用")) {
					Elements trs = e.getElementsByTag("tr");
					
					for(int i = 1;i<trs.size();i++) {
						Element tr = trs.get(i);
						Elements tds = tr.getElementsByTag("td");
						
						String[] content = {tds.get(0).ownText(),tds.get(1).ownText(),
								tds.get(2).ownText(),tds.get(3).ownText(),
								tds.get(4).ownText(),tds.get(5).ownText(),
								tds.get(6).ownText()};
						writer.writeRecord(content);
					}
				}
			}
		}
		writer.flush();
		writer.close();
	}
	/**
	 * 获取通话记录
	 * @param folder  需要爬取的 html集合的文件夹
	 * @param csvPath  csv文件输出地址
	 * @throws IOException
	 */
	public static void getContacts(String folder, String csvPath) throws IOException {
		List<String> htmls = getAllHtml(folder);
		
		System.out.println("-------获取通话记录---------");
		System.out.println("被处理的文件夹："+folder);
		System.out.println("csv文件输出路径："+csvPath);
		
		//通话记录
		String calls = csvPath+"通话记录.csv";
		CsvWriter writer = createCsvWriter(calls, 
				new String[] {"序号","时间","动作","对方","号码","内容"});
		
		for(String html : htmls) {
			File input = new File(folder+html);
			
			Document document = Jsoup.parse(input,"UTF-8");
			Elements datatable = document.getElementsByClass("datatable");
			
			for(Element e : datatable) {
				//每个表的第一行都是字段，根据字段的内容判断表的内容
				Element firstTr = e.getElementsByTag("tr").first();
				Elements th = firstTr.getElementsByTag("th");
				
				if(th.size()==6 && th.get(4).ownText().equals("号码")) {
					Elements trs = e.getElementsByTag("tr");
					
				for(int i = 1;i<trs.size();i++) {
					Element tr = trs.get(i);
					Elements tds = tr.getElementsByTag("td");
					String[] content = {tds.get(0).ownText(),tds.get(1).ownText(),
							tds.get(2).ownText(),tds.get(3).ownText(),
							tds.get(4).ownText(),tds.get(5).ownText()};
					
					if(!tds.get(2).ownText().contains("短信"))
						writer.writeRecord(content);//计入通话记录
					
					
				}
				
			}
				
			}
		}
		writer.flush();
		writer.close();
	}
	
	/**
	 * 获取短信
	 * @param folder  需要爬取的 html集合的文件夹
	 * @param csvPath  csv文件输出地址
	 * @throws IOException
	 */
	public static void getCalls(String folder, String csvPath) throws IOException {
		List<String> htmls = getAllHtml(folder);
		
		System.out.println("-------获取短信---------");
		System.out.println("被处理的文件夹："+folder);
		System.out.println("csv文件输出路径："+csvPath);
		
		//短信
		String message = csvPath+"短信.csv";
		CsvWriter writer = createCsvWriter(message, 
				new String[] {"序号","时间","动作","对方","号码","内容"});
		
		for(String html : htmls) {
			File input = new File(folder+html);
			
			Document document = Jsoup.parse(input,"UTF-8");
			Elements datatable = document.getElementsByClass("datatable");
			
			for(Element e : datatable) {
				//每个表的第一行都是字段，根据字段的内容判断表的内容
				Element firstTr = e.getElementsByTag("tr").first();
				Elements th = firstTr.getElementsByTag("th");
				
				
			}
		}
		writer.flush();
		writer.close();
	}
	 
	/**
	 * 遍历html文件，爬取需要的数据，并存入csv
	 * @param folder   "D:\\AHTMLDATA\\黄小雨862373030759292_20180207213918_报告\\Content\\"
	 * @param csvPath   需要存放csv的路径
	 * @throws IOException
	 */
	public static void getDataInCsv(String folder,String csvPath) throws IOException {
		
		getAppList(folder, csvPath);
		getContacts(folder, csvPath);
		getCalls(folder, csvPath);
		
	}
	
	
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		String folder = "D:\\AHTMLDATA\\黄小雨862373030759292_20180207213918_报告\\Content\\";
		String csvPath = "D:\\csvPath2\\";
		try {
			File file = new File(csvPath);
			if(!file.exists())
				file.mkdirs();
			
			getDataInCsv(folder,csvPath);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println("共耗时（ms）："+(end-start));

	}

}
